package com.goldcn.picture.service;

import com.goldcn.common.CommonTools;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.picture.dao.PicSysTagsItemMapper;
import com.goldcn.picture.dao.PicSysTagsMapper;
import com.goldcn.picture.dao.PicTagsMapper;
import com.goldcn.picture.dto.QueryPicTagDto;
import com.goldcn.picture.model.PicSysTags;
import com.goldcn.picture.model.PicSysTagsItem;
import com.goldcn.picture.model.PicTags;
import com.goldcn.picture.vo.PicSysTagsVo;
import com.goldcn.picture.vo.PicTagsVo;
import com.goldcn.picture.vo.SysTagsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PicTagsService {


    @Autowired
    PicSysTagsMapper picSysTagsMapper;

    @Autowired
    PicSysTagsItemMapper picSysTagsItemMapper;

    @Autowired
    PicTagsMapper picTagsMapper;


    /**
     * 添加系统图片标签
     * @param picSysTagsVo
     * @return
     */
    public Integer addPicSysTag(PicSysTagsVo picSysTagsVo){
        //添加标签组
        PicSysTags picSysTags=new PicSysTags();
        picSysTags.setAddtime(CommonUtils.getCurTimestamp());
        picSysTags.setTagid(IdGen.get().nextId());
        picSysTags.setTaggroup(picSysTagsVo.getTaggroup());
        picSysTags.setStatus(picSysTagsVo.getStatus());

        Integer result =  picSysTagsMapper.insertSelective(picSysTags);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.addLabelFailure);
        }
        //添加标签
        int index=0;
        if (picSysTagsVo.getTagname().size()>0){
            for (String name:picSysTagsVo.getTagname()){
                PicSysTagsItem picSysTagsItem=new PicSysTagsItem();
                picSysTagsItem.setTagitemid(IdGen.get().nextId());
                picSysTagsItem.setTagid(picSysTags.getTagid());
                picSysTagsItem.setTagname(name);
                picSysTagsItem.setSeq(index);
                picSysTagsItemMapper.insertSelective(picSysTagsItem);
                index++;
            }
        }
        return result;
    }


    /**
     * 更新图片标签
     * @param picSysTagsVo
     * @return
     */
    public Integer updateByPicLabel(PicSysTagsVo picSysTagsVo){

        PicSysTags picSysTags=new PicSysTags();
        BeanUtils.copyProperties(picSysTagsVo,picSysTags);

        Integer result =  picSysTagsMapper.updateByPrimaryKeySelective(picSysTags);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.updateLabelFailure);
        }

        int index=0;
        if (picSysTagsVo.getTagname().size()>0){
            picSysTagsItemMapper.deleteByTagID(picSysTagsVo.getTagid());
            for (String name:picSysTagsVo.getTagname()){
                PicSysTagsItem picSysTagsItem=new PicSysTagsItem();
                picSysTagsItem.setTagitemid(IdGen.get().nextId());
                picSysTagsItem.setTagid(picSysTags.getTagid());
                picSysTagsItem.setTagname(name);
                picSysTagsItem.setSeq(index);
                picSysTagsItemMapper.insertSelective(picSysTagsItem);
                index++;
            }
        }
        return result;
    }


    /**
     * 通过标签组ID删除标签组
     * @param tagid
     * @return
     */
    public Integer deleteByPrimaryKey(Long tagid){
        //判断标签是否使用
        if(picSysTagsMapper.tagUseCount(tagid)>0){
            throw CommonTools.createException(ErrorCodeEnum.lableIsUsed);
        }
        Integer result = picSysTagsMapper.deleteByPrimaryKey(tagid);
        if(result!=1){
            //抛出异常
            throw CommonTools.createException(ErrorCodeEnum.deleteLabelFailure);
        }
        //删除标签组下的所有标签
        picSysTagsItemMapper.deleteByTagID(tagid);

        return result;
    }

    /**
     * 根据ID查询标签
     * @param tagid
     * @return
     */
    public PicSysTagsVo selectByPrimaryKey(Long tagid){
        PicSysTags picSysTags=picSysTagsMapper.selectByPrimaryKey(tagid);
        PicSysTagsVo picSysTagsVo=new PicSysTagsVo();
        BeanUtils.copyProperties(picSysTags,picSysTagsVo);

        List<String> item=picSysTagsItemMapper.selectByTagId(tagid);
        picSysTagsVo.setTagname(item);

        return picSysTagsVo;
    }

    /**
     * 查询所有标签组
     * @return
     */
    public  List<PicSysTags> selectAllTags(QueryPicTagDto queryPicTagDto){
        return picSysTagsMapper.selectAllTags(queryPicTagDto);
    }

    /**
     * 查询所有标签组（系统管理员使用）
     * @return
     */
    public  List<SysTagsVo> selectListTags(){
        List<SysTagsVo> sysTagsVoList=new ArrayList<>();
        QueryPicTagDto  dto=new QueryPicTagDto();
        dto.setStatus(null);
        dto.setTaggroup(null);
        List<PicSysTags> sysTags=picSysTagsMapper.selectAllTags(dto);
        for(PicSysTags picSysTags:sysTags){
            SysTagsVo vo=new SysTagsVo();
            vo.setTaggroup(picSysTags.getTaggroup());
            vo.setTagid(picSysTags.getTagid());
            List<PicSysTagsItem> itemList=picSysTagsItemMapper.selectListByTagId(picSysTags.getTagid());
            if(itemList.size()>0){
                vo.setItemList(itemList);
                sysTagsVoList.add(vo);
            }
        }
        return sysTagsVoList;
    }


    /**
     * 图片库打系统标签
     * @param picTagsVo
     * @return
     */
    public Integer addPicSysTags(PicTagsVo picTagsVo){
        int result=0;
        if (picTagsVo.getItemid().size()>0){
            for (Long itemid:picTagsVo.getItemid()){
                PicSysTagsItem item=picSysTagsItemMapper.selectByPrimaryKey(itemid);
                if (item==null){
                    result=0;
                    break;
                }
                PicTags picTags=new PicTags();
                picTags.setPictagid(IdGen.get().nextId());
                picTags.setTagitemid(itemid);
                picTags.setPictureid(picTagsVo.getPictureid());
                result=picTagsMapper.insertSelective(picTags);
            }
        }
        if (result==0){
            throw CommonTools.createException(ErrorCodeEnum.updateSysFailure);
        }
        return result;
    }

    /**
     * 批量删除图片系统标签
     * @param picTagsVo
     * @return
     */
    public Object deleteByTagItemid(PicTagsVo picTagsVo){
        int result=0;
        if (picTagsVo.getItemid().size()>0){
            for (Long itemid:picTagsVo.getItemid()){
                if (picTagsVo.getPictureid()!=null && itemid!=null){
                    result=picTagsMapper.deleteByTagItemid(itemid,picTagsVo.getPictureid());
                }else{
                    throw CommonTools.createException(ErrorCodeEnum.delSysFailure);
                }
            }
        }
        return result;
    }

}
